package com.deepclassifieds.daas.api.builder;

import com.deepclassifieds.daas.api.xsd.ColumnDef;
import com.deepclassifieds.daas.api.xsd.ColumnDefs;
import com.deepclassifieds.daas.api.xsd.Datatype;
import com.deepclassifieds.daas.common.Builder;
import com.deepclassifieds.daas.common.Name;
import com.deepclassifieds.daas.common.Reject;

public class ColumnDefsBuilder implements Builder<ColumnDefs> {
	
	private final ColumnDefs columnDefs;
	
	public ColumnDefsBuilder() {
		this.columnDefs = new ColumnDefs();
	}
	
	public ColumnDefsBuilder(ColumnDefs columnDefs) {
		Reject.ifNull(columnDefs);
		this.columnDefs = new ColumnDefs();
		this.columnDefs.getColumnDef().addAll(columnDefs.getColumnDef());
	}

	@Override
	public ColumnDefs build() {
		return columnDefs;
	}

	public ColumnDef createAndAddColumnDef(Name columnName, Datatype datatype) {
		return createAndAddColumnDef(columnName, datatype, false, false, false);
	}

	public ColumnDef createAndAddIdentityColumnDef(Name columnName, Datatype datatype) {
		return createAndAddColumnDef(columnName, datatype, true, true, true);
	}

	public ColumnDef createAndAddColumnDef(Name columnName, Datatype datatype, boolean notNull, boolean unique, boolean indexed) {
		Reject.ifNull(columnName);
		ColumnDef columnDef = new ColumnDef();
		columnDef.setName(columnName.toString());
		columnDef.setDatatype(datatype);
		columnDef.setNotNull(notNull);
		columnDef.setUnique(unique);
		columnDef.setIndexed(indexed);
		columnDefs.getColumnDef().add(columnDef);
		return columnDef;
	}

	public ColumnDef createAndAddStringColumnDef(Name columnName, int maxLength) {
		Reject.ifNull(columnName);
		ColumnDef columnDef = new ColumnDef();
		columnDef.setDatatype(Datatype.STRING);
		columnDef.setMaxLength(maxLength);
		columnDef.setName(columnName.toString());
		columnDefs.getColumnDef().add(columnDef);
		return columnDef;
	}

}
